home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 December / maximum-cd-2009-12.iso / DiscContents / gimp-2.7.0-i686-setup.exe / {app} / lib / gimp / 2.0 / plug-ins / python-console.py < prev    next >
Encoding:
Python Source  |  2009-08-19  |  8.0 KB  |  230 lines

  1. #!/usr/bin/env python
  2.  
  3. #   Gimp-Python - allows the writing of Gimp plugins in Python.
  4. #   Copyright (C) 1997  James Henstridge <james@daa.com.au>
  5. #
  6. #   This program is free software: you can redistribute it and/or modify
  7. #   it under the terms of the GNU General Public License as published by
  8. #   the Free Software Foundation; either version 3 of the License, or
  9. #   (at your option) any later version.
  10. #
  11. #   This program is distributed in the hope that it will be useful,
  12. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #   GNU General Public License for more details.
  15. #
  16. #   You should have received a copy of the GNU General Public License
  17. #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. from gimpfu import *
  20.  
  21. t = gettext.translation('gimp20-python', gimp.locale_directory, fallback=True)
  22. _ = t.ugettext
  23.  
  24. PROC_NAME = 'python-fu-console'
  25.  
  26. RESPONSE_BROWSE, RESPONSE_CLEAR, RESPONSE_SAVE = range(3)
  27.  
  28. def do_console():
  29.     import pygtk
  30.     pygtk.require('2.0')
  31.  
  32.     import sys, gobject, gtk, gimpenums, gimpshelf, gimpui, pyconsole
  33.  
  34.     namespace = {'__builtins__': __builtins__,
  35.                  '__name__': '__main__', '__doc__': None,
  36.                  'gimp': gimp, 'pdb': gimp.pdb,
  37.                  'shelf': gimpshelf.shelf}
  38.  
  39.     for s in gimpenums.__dict__.keys():
  40.         if s[0] != '_':
  41.             namespace[s] = getattr(gimpenums, s)
  42.  
  43.     class GimpConsole(pyconsole.Console):
  44.         def __init__(self, quit_func=None):
  45.             banner = ('GIMP %s Python Console\nPython %s\n' %
  46.                       (gimp.pdb.gimp_version(), sys.version))
  47.             pyconsole.Console.__init__(self,
  48.                                        locals=namespace, banner=banner,
  49.                                        quit_func=quit_func)
  50.         def _commit(self):
  51.             pyconsole.Console._commit(self)
  52.             gimp.displays_flush()
  53.  
  54.     class ConsoleDialog(gimpui.Dialog):
  55.         def __init__(self):
  56.             gimpui.Dialog.__init__(self, title=_("Python Console"),
  57.                                    role=PROC_NAME, help_id=PROC_NAME,
  58.                                    buttons=(gtk.STOCK_SAVE,  RESPONSE_SAVE,
  59.                                             gtk.STOCK_CLEAR, RESPONSE_CLEAR,
  60.                                             _("_Browse..."), RESPONSE_BROWSE,
  61.                                             gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
  62.  
  63.             self.set_alternative_button_order((gtk.RESPONSE_CLOSE,
  64.                                                RESPONSE_BROWSE,
  65.                                                RESPONSE_CLEAR,
  66.                                                RESPONSE_SAVE))
  67.  
  68.             self.cons = GimpConsole(quit_func=lambda: gtk.main_quit())
  69.  
  70.             self.connect('response', self.response)
  71.  
  72.             self.browse_dlg = None
  73.             self.save_dlg = None
  74.  
  75.             vbox = gtk.VBox(False, 12)
  76.             vbox.set_border_width(12)
  77.             self.vbox.pack_start(vbox)
  78.  
  79.             scrl_win = gtk.ScrolledWindow()
  80.             scrl_win.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
  81.             vbox.pack_start(scrl_win)
  82.  
  83.             scrl_win.add(self.cons)
  84.  
  85.             self.set_default_size(500, 500)
  86.  
  87.         def response(self, dialog, response_id):
  88.             if response_id == RESPONSE_BROWSE:
  89.                 self.browse()
  90.             elif response_id == RESPONSE_CLEAR:
  91.                 self.cons.banner = None
  92.                 self.cons.clear()
  93.             elif response_id == RESPONSE_SAVE:
  94.                 self.save_dialog()
  95.             else:
  96.                 gtk.main_quit()
  97.  
  98.             self.cons.grab_focus()
  99.  
  100.         def browse_response(self, dlg, response_id):
  101.             if response_id != gtk.RESPONSE_APPLY:
  102.                 dlg.hide()
  103.                 return
  104.  
  105.             proc_name = dlg.get_selected()
  106.  
  107.             if not proc_name:
  108.                 return
  109.  
  110.             proc = pdb[proc_name]
  111.  
  112.             cmd = ''
  113.  
  114.             if len(proc.return_vals) > 0:
  115.                 cmd = ', '.join([x[1].replace('-', '_')
  116.                                 for x in proc.return_vals]) + ' = '
  117.  
  118.             cmd = cmd + 'pdb.%s' % proc.proc_name.replace('-', '_')
  119.  
  120.             if len(proc.params) > 0 and proc.params[0][1] == 'run-mode':
  121.                 params = proc.params[1:]
  122.             else:
  123.                 params = proc.params
  124.  
  125.             cmd = cmd + '(%s)' % ', '.join([x[1].replace('-', '_')
  126.                                            for x in params])
  127.  
  128.             buffer = self.cons.buffer
  129.  
  130.             lines = buffer.get_line_count()
  131.             iter = buffer.get_iter_at_line_offset(lines - 1, 4)
  132.             buffer.delete(iter, buffer.get_end_iter())
  133.             buffer.place_cursor(buffer.get_end_iter())
  134.             buffer.insert_at_cursor(cmd)
  135.  
  136.         def browse(self):
  137.             if not self.browse_dlg:
  138.                 dlg = gimpui.ProcBrowserDialog(_("Python Procedure Browser"),
  139.                                                role=PROC_NAME,
  140.                                                buttons=(gtk.STOCK_APPLY,
  141.                                                         gtk.RESPONSE_APPLY,
  142.                                                         gtk.STOCK_CLOSE,
  143.                                                         gtk.RESPONSE_CLOSE))
  144.  
  145.                 dlg.set_default_response(gtk.RESPONSE_APPLY)
  146.                 dlg.set_alternative_button_order((gtk.RESPONSE_CLOSE,
  147.                                                   gtk.RESPONSE_APPLY))
  148.  
  149.                 dlg.connect('response', self.browse_response)
  150.                 dlg.connect('row-activated',
  151.                             lambda dlg: dlg.response(gtk.RESPONSE_APPLY))
  152.  
  153.                 self.browse_dlg = dlg
  154.  
  155.             self.browse_dlg.present()
  156.  
  157.         def save_response(self, dlg, response_id):
  158.             if response_id == gtk.RESPONSE_DELETE_EVENT:
  159.                 self.save_dlg = None
  160.                 return
  161.             elif response_id == gtk.RESPONSE_OK:
  162.                 filename = dlg.get_filename()
  163.  
  164.                 try:
  165.                     logfile = open(filename, 'w')
  166.                 except IOError, e:
  167.                     gimp.message(_("Could not open '%s' for writing: %s") %
  168.                                  (filename, e.strerror))
  169.                     return
  170.  
  171.                 buffer = self.cons.buffer
  172.  
  173.                 start = buffer.get_start_iter()
  174.                 end = buffer.get_end_iter()
  175.  
  176.                 log = buffer.get_text(start, end, False)
  177.  
  178.                 try:
  179.                     logfile.write(log)
  180.                     logfile.close()
  181.                 except IOError, e:
  182.                     gimp.message(_("Could not write to '%s': %s") %
  183.                                  (filename, e.strerror))
  184.                     return
  185.  
  186.             dlg.hide()
  187.  
  188.         def save_dialog(self):
  189.             if not self.save_dlg:
  190.                 dlg = gtk.FileChooserDialog(_("Save Python-Fu Console Output"),
  191.                                             parent=self,
  192.                                             action=gtk.FILE_CHOOSER_ACTION_SAVE,
  193.                                             buttons=(gtk.STOCK_CANCEL,
  194.                                                      gtk.RESPONSE_CANCEL,
  195.                                                      gtk.STOCK_SAVE,
  196.                                                      gtk.RESPONSE_OK))
  197.  
  198.                 dlg.set_default_response(gtk.RESPONSE_OK)
  199.                 dlg.set_alternative_button_order((gtk.RESPONSE_OK,
  200.                                                   gtk.RESPONSE_CANCEL))
  201.  
  202.                 dlg.connect('response', self.save_response)
  203.  
  204.                 self.save_dlg = dlg
  205.  
  206.             self.save_dlg.present()
  207.  
  208.         def run(self):
  209.             self.show_all()
  210.             gtk.main()
  211.  
  212.     ConsoleDialog().run()
  213.  
  214. register(
  215.     PROC_NAME,
  216.     N_("Interactive GIMP Python interpreter"),
  217.     "Type in commands and see results",
  218.     "James Henstridge",
  219.     "James Henstridge",
  220.     "1997-1999",
  221.     N_("_Console"),
  222.     "",
  223.     [],
  224.     [],
  225.     do_console,
  226.     menu="<Image>/Filters/Languages/Python-Fu",
  227.     domain=("gimp20-python", gimp.locale_directory))
  228.  
  229. main()
  230.